package com.qubuyer.business.home.fragment

import androidx.lifecycle.MutableLiveData
import com.qubuyer.business.auth.model.AuthModel
import com.qubuyer.business.home.model.HomeModel
import com.qubuyer.core.anno.AutoInject
import com.qubuyer.core.framework.BaseViewModel
import com.qubuyer.core.http.RetrofitCall
import com.qubuyer.repository.entitys.*
import com.qubuyer.utils.SessionUtil

class HomeViewModel : BaseViewModel() {

    val bannerData = MutableLiveData<List<HomeBannerEntity>>()
    val cateData = MutableLiveData<List<HomeCategoryEntity>>()
    val saleTopData = MutableLiveData<HomeSaleTopEntity>()
    val userData = MutableLiveData<UserEntity>()
    val choiceData = MutableLiveData<List<HomeGoodEntity>>()
    val limitData = MutableLiveData<List<HomeGoodEntity>>()
    val specData = MutableLiveData<List<HomeGoodEntity>>()
    val publishData = MutableLiveData<List<HomeGoodEntity>>()
    val superReturnData = MutableLiveData<List<HomeGoodEntity>>()
    val dailyDiscountData = MutableLiveData<List<HomeGoodEntity>>()
    val unreadMsgCount = MutableLiveData<Int>()

    @AutoInject
    private val model = HomeModel()

    @AutoInject
    private val authModel = AuthModel()

    private var page = 1

    fun refresh(block: (success: Boolean, dataList: List<HomeGoodEntity>?, page: Int, fromCache: Boolean) -> Unit) {
        loadBanner()
        loadCategory()
        loadSaleTop()
        loadUserInfo()
        loadChoiceGoodInfo()
        loadLimitGoodInfo()
        loadSpecGoodInfo()
        loadPublishGoodInfo()
        loadSuperReturnGoodInfo()
        loadDailyDiscountGoodInfo()
        page = 1
        RetrofitCall<List<HomeGoodEntity>?>(lifecycleOwner, this) {
            showDialog { false }
            api { model.hotRecommend(page) }
            success {
                block.invoke(true, it, page, false)
            }
            fail { list, _, _ ->
                block.invoke(false, list, page, false)
            }
        }
    }

    fun loadMore(block: (success: Boolean, dataList: List<HomeGoodEntity>?, page: Int, fromCache: Boolean) -> Unit) {
        page++
        RetrofitCall<List<HomeGoodEntity>?>(lifecycleOwner, this) {
            showDialog { false }
            api { model.hotRecommend(page) }
            success {
                block.invoke(true, it, page, false)
            }
            fail { list, _, _ ->
                block.invoke(false, list, page, false)
            }
        }
    }

    private fun loadBanner() {
        RetrofitCall<List<HomeBannerEntity>?>(lifecycleOwner, this) {
            showDialog { false }
            showToast { false }
            api { model.loadBanner() }
            success {
                bannerData.value = it
            }
        }
    }

    private fun loadCategory() {
        RetrofitCall<List<HomeCategoryEntity>?>(lifecycleOwner, this) {
            showDialog { false }
            api { model.loadCate() }
            success {
                cateData.value = it
            }
        }
    }

    private fun loadSaleTop() {
        RetrofitCall<HomeSaleTopEntity?>(lifecycleOwner, this) {
            showDialog { false }
            api { model.loadSaleTop() }
            success {
                saleTopData.value = it
            }
        }
    }

    private fun loadUserInfo() {
        if(SessionUtil.getInstance().isLogined){
            RetrofitCall<UserEntity?>(lifecycleOwner, this) {
                showDialog { false }
                api { authModel.userInfo() }
                success {
                    userData.value = it
                }
            }
        }
    }

    private fun loadChoiceGoodInfo() {
        RetrofitCall<List<HomeGoodEntity>?>(lifecycleOwner, this) {
            showDialog { false }
            api { model.loadGoodInfo("fine") }
            success {
                choiceData.value = it
            }
        }
    }

    private fun loadSpecGoodInfo() {
        RetrofitCall<List<HomeGoodEntity>?>(lifecycleOwner, this) {
            showDialog { false }
            api { model.loadGoodInfo("hot") }
            success {
                specData.value = it
            }
        }
    }

    private fun loadPublishGoodInfo() {
        RetrofitCall<List<HomeGoodEntity>?>(lifecycleOwner, this) {
            showDialog { false }
            showToast { false }
            api { model.loadGoodInfo("new") }
            success {
                publishData.value = it
            }
        }
    }

    private fun loadLimitGoodInfo() {
        RetrofitCall<List<HomeGoodEntity>?>(lifecycleOwner, this) {
            showDialog { false }
            api { model.loadLimitGoodInfo() }
            success {
                limitData.value = it
            }
        }
    }

    private fun loadSuperReturnGoodInfo() {
        RetrofitCall<List<HomeGoodEntity>?>(lifecycleOwner, this) {
            showDialog { false }
            api { model.loadSuperReturnGoodInfo() }
            success {
                superReturnData.value = it
            }
        }
    }

    private fun loadDailyDiscountGoodInfo() {
        RetrofitCall<List<HomeGoodEntity>?>(lifecycleOwner, this) {
            showDialog { false }
            api { model.loadDailyDiscountGoodInfo() }
            success {
                dailyDiscountData.value = it
            }
        }
    }

    private fun loadUnreadMsgCount() {
        RetrofitCall<UserMessageCountEntity?>(lifecycleOwner, this) {
            showDialog { false }
            api { model.unSendNumber() }
            success {
                unreadMsgCount.value = it?.count ?: 0
            }
        }
    }
}